namespace Sharpmark.PerfectLife.ColorfulPhotos.TextualPhoto.SubFunctionForm
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using Sharpmark.PerfectLife.Core.Picture;
    using Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText;

    public partial class FrmPhotoHtmlText : Form
    {
        private const int DEFAULT_PHOTO_WIDTH = 200;
        private const int DEFAULT_PHOTO_HEIGHT = 200;

        private PhotoHtmlText _photo;				// 读入的照片
        private bool _isKeepScale;					// 是否保持长宽比不变

        public FrmPhotoHtmlText()
        {
            InitializeComponent();

            _photo = new PhotoHtmlText(
                new Photo((int)nudCharsWidth.Value, (int)nudCharsHeight.Value),
                (int)nudCharsWidth.Value, (int)nudCharsHeight.Value,
                (txtReplaceChar.Text != "") ? txtReplaceChar.Text[0] : ReplaceCharsList.Chars1[0],
                fdFontSelect.Font,
                this.ckbBackColorEnabled.Checked);

            _isKeepScale = this.ckbIsKeepScale.Checked;

            //wbbGeneratedText.

            SetControlsEnabled(false);
        }

        private void btnBrowerFile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.odOpenImageFile.ShowDialog())
            {
                this.txtFileName.Text = this.odOpenImageFile.FileName;
            }
        }

        private void nudCharsWidth_ValueChanged(object sender, EventArgs e)
        {
            _photo.CharsWidth = (int)nudCharsWidth.Value;
            if (_isKeepScale)
                nudCharsHeight.Value = ScaleHeightByWidth(nudCharsWidth.Value);
        }

        private void nudCharsHeight_ValueChanged(object sender, EventArgs e)
        {
            _photo.CharsHeight = (int)nudCharsHeight.Value;
            if (_isKeepScale)
                nudCharsWidth.Value = ScaleWidthByHeight(nudCharsHeight.Value);
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(Path.GetTempPath() + "colorfulPhoto.htm", false);
            sw.Write(_photo.GenerateText());
            sw.Close();
            this.wbbGeneratedText.Navigate(Path.GetTempPath() + "colorfulPhoto.htm");
        }

        private void ckbIsKeepScale_CheckedChanged(object sender, EventArgs e)
        {
            _isKeepScale = ckbIsKeepScale.Checked;
            // 激活改变事件，使得长宽比自动调节
            nudCharsWidth_ValueChanged(nudCharsWidth, new EventArgs());
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            if (File.Exists(this.txtFileName.Text))
            {
                _photo.Photo = new Photo(this.txtFileName.Text);

                // 当前文件可用，则将控件设为可用
                SetControlsEnabled(true);

                // 计算图像值
                nudCharsWidth.Value = _photo.Photo.Width / 3;
                if (!_isKeepScale)
                    nudCharsHeight.Value = _photo.Photo.Height / 5;
            }
            else
            {
                // 当前文件不可用，则将控件设为不可用
                SetControlsEnabled(false);
            }
        }

        private void SetControlsEnabled(bool enabled)
        {
            gpbCharsSize.Enabled = enabled;
            gpbReplaceSetting.Enabled = enabled;
            btnGenerate.Enabled = enabled;
            btnThumbnailView.Enabled = enabled;
            // btnSave什么时候都可用。
            // btnSave.Enabled = enabled;
        }

        private void btnThumbnailView_Click(object sender, EventArgs e)
        {
            (new FrmThumbnailView(txtFileName.Text)).Show();
        }

        private void textBox_SelectAll(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void numbericUpDown_SelectAll(object sender, EventArgs e)
        {
            NumericUpDown nup = (NumericUpDown)sender;
            nup.Select(0, nup.Value.ToString().Length);
        }

        private decimal ScaleWidthByHeight(decimal h)
        {
            // 同比缩放长宽
            // *3 / 5 是因为大多数字体长宽比5:3，为了使图像显示比较正常
            return _photo.Photo.Width * h * 5 / _photo.Photo.Height / 3;
        }

        private decimal ScaleHeightByWidth(decimal w)
        {
            // 同比缩放长宽
            return _photo.Photo.Height * w * 3 / _photo.Photo.Width / 5;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            wbbGeneratedText.ShowSaveAsDialog();

            //TODO: 该不该加保存完成的提示框？
            // MessageBox.Show(""
        }

        private void txtReplaceChar_TextChanged(object sender, EventArgs e)
        {
            if (txtReplaceChar.Text != "")
            {
                _photo.ReplaceChar = txtReplaceChar.Text[0];
                SetControlsEnabled(true);
            }
            else
            {
                SetControlsEnabled(false);
            }
        }

        private void ckbBackColorEnabled_CheckedChanged(object sender, EventArgs e)
        {
            _photo.EnableFontBackColor = ckbBackColorEnabled.Checked;
        }

        private void btnFontSetting_Click(object sender, EventArgs e)
        {
            fdFontSelect.ShowDialog();
            _photo.Font = fdFontSelect.Font;
        }

        ~FrmPhotoHtmlText()
        {
            if (File.Exists(Path.GetTempPath() + "colorfulPhoto.htm"))
                File.Delete(Path.GetTempPath() + "colorfulPhoto.htm");
        }
    }
}